from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from goods.models import SKUImage, SKU
from meiduo_admin.serialziers.images import ImageSerialier, SKUSeriazlier
from meiduo_admin.utils import PageNum


class ImagesView(ModelViewSet):
    # 图片序列化器
    serializer_class = ImageSerialier
    # 查询集
    queryset = SKUImage.objects.all()
    # 分页
    pagination_class = PageNum
    permission_classes = [IsAdminUser]

    def simple(self, request):
        data = SKU.objects.all()
        ser = SKUSeriazlier(data, many=True)
        return Response(ser.data)

        # def create(self, request, *args, **kwargs):
        #     # 获取前端数据
        #     data=request.data
        #     # 验证数据
        #     ser = self.get_serializer(data=data)
        #     ser.is_valid()
        #     # # 建立fastdfs客户端连接
        #     # client=Fdfs_client(settings.FASTDFS_PATH)
        #     # # 获取图片
        #     # image_file=request.FILES.get('image')
        #     # # 上传图片
        #     # res=client.upload_by_buffer(image_file.read())
        #     # # 判断是否上传成功
        #     # if res['Status'] != 'Upload successed.':
        #     #     return Response({'error':'图片上传失败'})
        #     # # 获取sku_id
        #     # sku_id=request.data.get('sku')
        #     # # 获取dfs上传之后的图片地址
        #     # image_url=res['Remote file_id']
        #     # img=SKUImage.objects.create(sku_id=sku_id,image=image_url)
        #     ser.save()
        #     # 返回数据
        #     return Response(ser.data,status=201)
